﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;

namespace DAXT
{
    public partial class FormResetConfiguration : Form
    {
        RegistryKey     rootKey;
        ArrayList       axKeysListNames = new ArrayList();
        StringBuilder sb = new StringBuilder();

        public FormResetConfiguration()
        {
            InitializeComponent();
        }

        private void parseKey(RegistryKey key)
        {
            foreach (string name in key.GetValueNames())
            {
                if (name.ToLower() == "aos2") // setting value found in the client cfg and client setup
                {
                    if (!axKeysListNames.Contains(key.Name))
                    { 
                        axKeysListNames.Add(key.Name);
                    }

                    break;
                }

                if (name.ToLower() == "xppdebug") // setting found in server cfg
                {
                    if (!axKeysListNames.Contains(key.Name))
                    {
                        axKeysListNames.Add(key.Name);
                    }

                    break;
                }
            }
            
            string[] subKeys = key.GetSubKeyNames();

            for (int i = 0; i < subKeys.Length; i++)
            {
                string subKeyName = subKeys[i];

                try
                { 
                    RegistryKey subKey = key.OpenSubKey(subKeyName);

                    this.parseKey(subKey);

                    subKey.Close();
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }

        private void SearchButton_Click(object sender, EventArgs e)
        {
            rootKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64);

            this.parseKey(rootKey);

            rootKey.Close();

            rootKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);

            this.parseKey(rootKey);

            rootKey.Close();

            rootKey = RegistryKey.OpenBaseKey(RegistryHive.Users, RegistryView.Registry64);

            this.parseKey(rootKey);

            rootKey.Close();

            foreach (string name in axKeysListNames)
            {
                sb.AppendLine(name);
            }

            KeysListTextBox.Text = sb.ToString();
        }

        private void changeValue(RegistryKey key, string valueName)
        {
            object o = key.GetValue(valueName);

            if (o != null)
            {
                key.SetValue(valueName, o.ToString().ToLower().Replace(replaceFromTextBox.Text.ToLower(), replaceWithTextBox.Text.ToLower()));
            }
        }

        private void ReplaceButton_Click(object sender, EventArgs e)
        {
            if (replaceWithTextBox.Text == "NEW server name" ||
                replaceFromTextBox.Text == "OLD server name")
            {
                MessageBox.Show("Change your server names and retry.");
                return;
            }

            if (MessageBox.Show("Use at your own risk: I am not to be held responsible for ANY damage or loss of data. DO NOT USE it on production / live servers. Do you wish to continue?",
                    "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes)
            {
                foreach (string keyName in axKeysListNames)
                {
                    if (keyName.IndexOf("HKEY_USERS") != -1)
                    {
                        rootKey = RegistryKey.OpenBaseKey(RegistryHive.Users, RegistryView.Registry64);
                    }
                    else
                        if (keyName.IndexOf("HKEY_LOCAL_MACHINE") != -1)
                        {
                            rootKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
                        }
                        else
                            if (keyName.IndexOf("HKEY_CURRENT_USER") != -1)
                            {
                                rootKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64);
                            }

                    RegistryKey key = rootKey.CreateSubKey(keyName.Replace(rootKey.Name + "\\", ""));

                    if (key != null)
                    {
                        this.changeValue(key, "aos2");
                        this.changeValue(key, "dbserver");
                        this.changeValue(key, "internet");
                        this.changeValue(key, "wcfconfig");
                        this.changeValue(key, "clientaosserver");

                        key.Close();
                    }

                    rootKey.Close();
                }
                
                MessageBox.Show("Done");
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }
    }
}
